<?php

class Application_Model_FinancialCheckItem extends Application_Model_DomainObject {
    
	protected $_id;
        
        protected $_objectType;
        
	protected $_objectId;
        
        protected $_money;
        
        protected $_reason;
        
        protected $_dateCreated;
        
        protected $_status;
        
        protected $_applyName;
        
        protected $_userId;
        
        protected $_dateChecked;
        
        protected $_needAddMoney;
        
        protected $_comment;
        
        /**
         * 自动审核的最高标准
         */
        const Auto_Money = 100;
        
        /**
         * 扣款
         */
        const Money_Need = 1;
        /**
         * 充值
         */
        const Money_Add = 2;
        /**
         * 代购损耗
         */
        const Money_Loss = 3;
        /**
         * 退换货卖家承担
         */
        const Money_ReturnSeller = 4;
        /**
         * 退换货悠奇承担
         */
        const Money_ReturnUuch = 5;
        /**
         * 代购退货退还费
         */
        const Money_orderReturn = 6;
        /**
         * 二次支付
         */
        const Money_twoPay = 7;
        /**
         * 自动审核
         */
        const Status_Auto = 1;
        /**
         * 待审核
         */
        const Status_Pending = 2;
        /**
         * 已审核
         */
        const Status_Trial = 3;
        /**
         * 驳回
         */
        const Status_Reject = 4;
        /**
         * 关闭
         */
        const Status_Close = 10;
        /**
         * 驳回关闭
         */
        const status_RejectClose = 40;
        
        const Operator_Default = "李桂芳";


        public static function getMoneyTypeAll()
        {
            return array(
                       self::Money_Need => "代购手工扣款"
                    ,  self::Money_Add => "代购手工退款"
                    ,  self::Money_Loss => "代购员工损耗"
                    ,  self::Money_ReturnSeller => "退换货运费卖家承担"
                    ,  self::Money_ReturnUuch => "退换货运费悠奇承担"
                    ,  self::Money_orderReturn => "代购退货退款"
                    ,  self::Money_twoPay => "前台用户二次支付"
            );
        }
        public function getMoneyTypeName()
        {
            $moneyTypeAll = self::getMoneyTypeAll();
            return $moneyTypeAll[$this->_needAddMoney];
        }
        
        public static function getStatusAll()
        {
            return array(
                       self::Status_Auto => "自动审核"
                    ,  self::Status_Pending => "待审核"
                    ,  self::Status_Trial => "已审核"
                    ,  self::Status_Reject => "驳回"
                    ,  self::Status_Close => "关闭"
            );
        }
        
        public function getStatusName()
        {
            $statusAll = self::getStatusAll();
            return $statusAll[$this->_status];
        }
        
        public static function getOnebyObjectTypeObjectId($objectType,$objectId)
        {
            return self::getDao()->queryOne(array("objectType = "=>$objectType," and objectId = "=>$objectId," and NeedAddMoney in "=>"(".self::Money_Need.",".self::Money_Add.")"));
        }
        
        public static function getAllbyObjectTypeObjectId($objectType,$objectId)
        {
            return self::getDao()->queryAll(array("objectType = "=>$objectType," and objectId = "=>$objectId));
        }
        
        public static function addCheckItem($objectType,$objectId,$money,$reason,$applyName,$userId,$needAddMoney)
        {
            $newCheckItem = new Application_Model_FinancialCheckItem();
            $newCheckItem->ObjectType = $objectType;
            $newCheckItem->ObjectId = $objectId;
            $newCheckItem->Money = $money;
            $newCheckItem->Reason = $reason;
            $newCheckItem->DateCreated = date("Y-m-d H:i:s");
//            $status = $money<=self::Auto_Money?self::Status_Auto:self::Status_Pending;
            $status = self::Status_Pending;
            $newCheckItem->Status = $status;
            $newCheckItem->ApplyName = $applyName;
            $newCheckItem->UserId = $userId;
            $newCheckItem->NeedAddMoney = $needAddMoney;
            $newCheckItem->save();
            return $newCheckItem;
        }
        
        public static function getOneByObjectMonetType($objectType,$objectId,$moneyType)
        {
            return self::getDao()->queryOne(array("objectType = "=>$objectType," and objectId = "=>$objectId," and NeedAddMoney = "=>$moneyType));
        }
        
        public static function countFinByStatu($statu)
        {
            return self::getDao()->count(array("status = "=>$statu));
        }
        
        public static function getFilter($financialFilter,$pagesize,$pageindex)
        {
//            var_dump($financialFilter->getSearchFileds());
            return self::getDao()->queryPage($financialFilter->getSearchFileds(), "id desc", $pagesize, $pageindex);
        }
        
        public static function getFilterAll($financialFilter)
        {
            return self::getDao()->queryAll($financialFilter->getSearchFileds(), "id desc");
        }
        
        public function getObjectItem()
        {
            return Application_Model_DomainobjectType::load($this->_objectType);
        }
        
        public function getObjectRelevance()
        {
            return Application_Model_DomainobjectType::getTypeItem($this->_objectType);
        }
        
        public function getUser()
        {
            return Application_Model_User::load($this->_userId);
        }
        public function getOrderTaobaoId()
        {
              $ret = Application_Model_Order::load($this->_objectId);
            return $ret->ProcureNo;
        }
       
}